Namespace GUI

    Friend Class MirButton
        Implements Windows.GUIObj

        Dim mWnd As MirWindow

        Dim mName As String
        Dim mLib As EvoLib.Protocol.ImgLibs
        Dim mNormalImg As Integer
        Dim mHoverImg As Integer
        Dim mDownImg As Integer
        Dim mToolTipText As String
        Dim mTextString As String
        Dim mTextColor As Color
        Dim mButtonWidth As Integer

        Dim mPosX As Integer, mPosY As Integer, mPosZ As Single

        Dim mClientRect As Rectangle
        Dim mIsVisible As Boolean
        Dim mGetsFocus As Boolean

        Public Event Click()
        Public Event RequestingTooltip(tttxt As String)

        Public Sub New(ByRef wnd As MirWindow, ByRef name As String, ByRef x As Integer, ByRef y As Integer, ByRef z As Single, _
                        ByRef imglib As EvoLib.Protocol.ImgLibs, ByRef normalimg As Integer, Optional ByRef hoverimg As Integer = 0, _
                        Optional ByRef downimg As Integer = 0, Optional ByRef tooltiptext As String = "", Optional ByRef getsfocus As Boolean = True, Optional buttontext As String = "", Optional textcol As Color = Nothing, Optional buttonwidth As Integer = -1)

            mWnd = wnd
            mName = name
            mPosX = x
            mPosY = y
            mPosZ = z
            mLib = imglib
            mNormalImg = normalimg
            mButtonWidth = buttonwidth

            mTextString = buttontext

            If textcol = Color.Empty Then
                textcol = Color.Yellow
            End If
            mTextColor = textcol


            If hoverimg > 0 Then
                mHoverImg = hoverimg
            Else
                mHoverImg = mNormalImg
            End If

            If downimg > 0 Then
                mDownImg = downimg
            Else
                mDownImg = mHoverImg
            End If

            mToolTipText = tooltiptext

            CalcClientRect()

            mIsVisible = True

            mGetsFocus = getsfocus

            wnd.AddInterfaceItem(Me)

        End Sub

        Public Sub MouseInput(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs) Implements Windows.mWindows.GUIObj.MouseInput

            Select Case type

                Case Mouse.MouseInputType.MouseClick
                    RaiseEvent Click()

                Case Mouse.MouseInputType.MouseMove
                    RaiseEvent RequestingTooltip(mToolTipText)

            End Select

        End Sub

        Private Sub CalcClientRect()

            Dim w As Integer, h As Integer

            w = Libs.GetLib(mLib).Image(mNormalImg).Header.Width
            h = Libs.GetLib(mLib).Image(mNormalImg).Header.Height

            With Libs.GetLib(mLib).Image(mHoverImg).Header
                If w < .Width Or Not Libs.GetLib(mLib).Image(mNormalImg).HasImage Then w = .Width
                If h < .Height Or Not Libs.GetLib(mLib).Image(mNormalImg).HasImage Then h = .Height
            End With

            With Libs.GetLib(mLib).Image(mDownImg).Header
                If w < .Width Or Not Libs.GetLib(mLib).Image(mHoverImg).HasImage Then w = .Width
                If h < .Height Or Not Libs.GetLib(mLib).Image(mHoverImg).HasImage Then h = .Height
            End With

            If mButtonWidth > 0 Then w = mButtonWidth

            mClientRect = New Rectangle(mPosX, mPosY, w, h)

        End Sub

        Public Function Contains(ByRef x As Integer, ByRef y As Integer) As Boolean Implements Windows.mWindows.GUIObj.Contains

            If Not mIsVisible Then Return False
            If mClientRect.Contains(x, y) Then

                Dim checkImg As Integer
                If Libs.GetLib(mLib).Image(mNormalImg).Texture Is Nothing Then
                    checkImg = mHoverImg
                Else
                    checkImg = mNormalImg
                End If

                If TexContains(Libs.GetLib(mLib).Image(checkImg).Texture, x - mClientRect.Left, y - mClientRect.Top) Then
                    Return True
                Else
                    Return False
                End If

            End If

        End Function

        Private Function TexContains(ByRef tex As Microsoft.DirectX.Direct3D.Texture, ByRef x As Integer, ByRef y As Integer) As Boolean

            Try

                If tex Is Nothing Then Return False

                Dim ranks() As Integer = {4}
                Dim arr() As Byte = tex.LockRectangle(GetType(Byte), 0, New Rectangle(x, y, 1, 1), Microsoft.DirectX.Direct3D.LockFlags.ReadOnly, ranks)
                tex.UnlockRectangle(0)

                If arr(0) > 0 And arr(1) > 0 And arr(2) > 0 Then
                    Return True
                Else
                    Return False
                End If

            Catch ex As Exception
                Return False
            End Try

        End Function

        Public Sub Draw(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Implements Windows.mWindows.GUIObj.Draw

            If mPosZ <> 0.0F Then z = mPosZ

            If Not mIsVisible Then Exit Sub

            If mode = Globals.DrawingMode.Normal Then

                With Libs.GetLib(mLib)

                    If Mouse.IsOver(OffsetRect(mClientRect, x, y)) And Not Windows.BlockInputs(mWnd) And mWnd Is Windows.GetWndAtPos(Mouse.XCoord, Mouse.YCoord) Then
                        Dim checkImg As Integer
                        If .Image(mNormalImg).Texture Is Nothing Then
                            checkImg = mHoverImg
                        Else
                            checkImg = mNormalImg
                        End If

                        If TexContains(.Image(checkImg).Texture, Mouse.XCoord - x - mPosX, Mouse.YCoord - y - mPosY) Then
                            If Mouse.bIsDown Then
                                'down
                                .Image(mDownImg).Draw(x + mPosX, y + mPosY, z)
                            Else
                                'draw hover
                                .Image(mHoverImg).Draw(x + mPosX, y + mPosY, z)
                            End If
                        Else
                            'norm
                            .Image(mNormalImg).Draw(x + mPosX, y + mPosY, z)
                        End If
                    Else
                        'norm
                        .Image(mNormalImg).Draw(x + mPosX, y + mPosY, z)
                    End If

                End With

            ElseIf mode = Globals.DrawingMode.Text Then

                If Not mTextString Is Nothing Then
                    If mTextString.Length > 0 Then

                        Dim tW As Integer = DirectX.ProFont9.GetNormalTextWidth(mTextString)
                        Dim tH As Integer = DirectX.ProFont9.GetNormalTextHeight(mTextString)

                        DirectX.ProFont9.DrawBorderText(mTextString, x + mPosX + (mClientRect.Width / 2) - (tW / 2), y + mPosY + (mClientRect.Height / 2) - (tH / 2) + 2, mTextColor, Color.Black, z) '- 0.05F)

                    End If
                End If

                End If

        End Sub

        Public Property Visible() As Boolean
            Get
                Return mIsVisible
            End Get
            Set(ByVal value As Boolean)
                mIsVisible = value
            End Set
        End Property

        Public Sub Focus() Implements Windows.mWindows.GUIObj.Focus

        End Sub

        Public Sub KeyboardInput(ByRef type As Keyboard.mKeyboardVars.KeyboardInputType, ByRef e As System.Windows.Forms.KeyEventArgs) Implements Windows.mWindows.GUIObj.KeyboardInput

        End Sub

        Public Sub KeyboardInput(ByRef type As Keyboard.mKeyboardVars.KeyboardInputType, ByRef e As System.Windows.Forms.KeyPressEventArgs) Implements Windows.mWindows.GUIObj.KeyboardInput

        End Sub

        Public Event TabSwitch() Implements Windows.mWindows.GUIObj.TabSwitch

        Public Sub UnFocus() Implements Windows.mWindows.GUIObj.UnFocus

        End Sub

        Public Sub Destroy() Implements Windows.mWindows.GUIObj.Destroy
            mWnd.RemoveInterfaceItem(Me)
        End Sub

        Public ReadOnly Property GetsFocus() As Boolean Implements Windows.mWindows.GUIObj.GetsFocus
            Get
                Return mGetsFocus
            End Get
        End Property

        Public Sub SetPos(ByRef x As Integer, ByRef y As Integer) Implements Windows.mWindows.GUIObj.SetPos
            mPosX = x
            mPosY = y
            CalcClientRect()
        End Sub

        Public ReadOnly Property ParentWin() As MirWindow Implements Windows.mWindows.GUIObj.ParentWin
            Get
                Return mWnd
            End Get
        End Property

        Public ReadOnly Property Name() As String Implements Windows.mWindows.GUIObj.Name
            Get
                Return mName
            End Get
        End Property

        Public Sub ReloadSurafces() Implements Windows.mWindows.GUIObj.ReloadSurafces

        End Sub
    End Class

End Namespace